home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug172 / lexsrt.c < prev    next >
C/C++ Source or Header  |  1986-02-05  |  3KB  |  136 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     LEX - A Lexical Analyser Generator
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Jan 30, 1985
  6.   DESCRIPTION:     A Lexical Analyser Generator. From UNIX
  7.   KEYWORDS:     Lexical Analyser Generator YACC C PREP
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      LEXSRT.C
  10.   WARNINGS:     This program is not for the casual user. It will
  11.          be useful primarily to expert developers.
  12.   CRC:         N/A
  13.   SEE-ALSO:     YACC and PREP
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18. /*
  19.  * Quick Sort
  20.  */
  21.  
  22. int     (*qs__cmp)();
  23. static  int     size;
  24.  
  25. qsort(a, n, es, fc)
  26. char *a;
  27. int n, es;
  28. int (*fc)();
  29. {
  30.         qs__cmp = fc;
  31.         size = es;
  32.         q_sort(a, a+n*es);
  33. }
  34.  
  35. q_sort(a, l)
  36. char *a, *l;
  37. {
  38.         register char *i, *j;
  39.         register int es;
  40.         char *lp, *hp;
  41.         int n, c;
  42.  
  43.         es = size;
  44.  
  45. start:
  46.         if((n=l-a) <= es)
  47.                 return;
  48.         n = es * ( n/(2*es));
  49.         hp = lp = a+n;
  50.         i = a;
  51.         j = l-es;
  52.         for(;;) {
  53.                 if(i < lp) {
  54.                         if((c = (*qs__cmp)(i, lp)) == 0) {
  55.                                 q_exc(i, lp -= es);
  56.                                 continue;
  57.                         }
  58.                         if(c < 0) {
  59.                                 i += es;
  60.                                 continue;
  61.                         }
  62.                 }
  63.  
  64. loop:
  65.                 if(j > hp) {
  66.                         if((c = (*qs__cmp)(hp, j)) == 0) {
  67.                                 q_exc(hp += es, j);
  68.                                 goto loop;
  69.                         }
  70.                         if(c > 0) {
  71.                                 if(i == lp) {
  72.                                         q_tex(i, hp += es, j);
  73.                                         i = lp += es;
  74.                                         goto loop;
  75.                                 }
  76.                                 q_exc(i, j);
  77.                                 j -= es;
  78.                                 i += es;
  79.  
  80.                                 continue;
  81.                         }
  82.                         j -= es;
  83.                         goto loop;
  84.                 }
  85.  
  86.  
  87.                 if(i == lp) {
  88.                         if(lp-a >= l-hp) {
  89.                                 q_sort(hp+es, l);
  90.                                 l = lp;
  91.                         } else {
  92.                                 q_sort(a, lp);
  93.                                 a = hp+es;
  94.                         }
  95.                         goto start;
  96.                 }
  97.                 q_tex(j, lp -= es, i);
  98.                 j = hp -= es;
  99.         }
  100. }
  101.  
  102. q_exc(i, j)
  103. char *i, *j;
  104. {
  105.         register char *ri, *rj, c;
  106.         int n;
  107.  
  108.         n = size;
  109.         ri = i;
  110.         rj = j;
  111.         do {
  112.                 c = *ri;
  113.                 *ri++ = *rj;
  114.                 *rj++ = c;
  115.         } while(--n);
  116. }
  117.  
  118. q_tex(i, j, k)
  119. char *i, *j, *k;
  120. {
  121.         register char *ri, *rj, *rk;
  122.         int c;
  123.         int n;
  124.  
  125.         n = size;
  126.         ri = i;
  127.         rj = j;
  128.         rk = k;
  129.         do {
  130.                 c = *ri;
  131.                 *ri++ = *rk;
  132.                 *rk++ = *rj;
  133.                 *rj++ = c;
  134.         } while(--n);
  135. }
  136.